package com.ikkong.qy.util;

import java.io.File;
import java.net.URL;
import java.util.Date;

import org.apache.poi.ss.usermodel.DateUtil;

import com.fj.hiwetoptools.FileUtil;
import com.fj.hiwetoptools.StringUtil;
import com.ikkong.core.constant.Cst;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Func;
import com.ikkong.system.model.Attach;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PathKit;

/**
 * 微信接口的封装
 * @author wukh
 * @2016年11月30日
 */
public class WeixinInterface {

	/**
	 * 创建群聊
	 * @param chatId
	 * @param owner
	 * @param userIds
	 * @param chatToken
	 * @param chatName
	 * @return
	 */
	public static String[] createChat(String chatId,String owner,String[] userIds,String chatToken,String chatName){
		StringBuilder sb = new StringBuilder();
		sb.append("{\"chatid\": \"");
		sb.append(chatId);
		sb.append("\",\"name\": \"");
		sb.append(chatName);
		sb.append("\",\"owner\": \"");
		sb.append(owner);
		sb.append("\",\"userlist\":[\"");
		
		for (int i = 0; i < userIds.length; i++) {			
			if(!JStrKit.isBlank(userIds[i])){
				sb.append("\"");
				sb.append(userIds[i]);
				sb.append("\"");
				sb.append(",");
			}
		}
		sb.setLength(sb.length()-1);
		sb.append("\"]}");
		
		String url = "https://qyapi.weixin.qq.com/cgi-bin/chat/create?access_token=" + chatToken;
		
		String[] strArray = new String[3];
		strArray[0] = url;
		strArray[1] = sb.toString();
		strArray[2] = HttpKit.post(url, sb.toString());
		
		return strArray;
	}
	

	/**
	 * 推送消息
	 * @param agentId		应用ID
	 * @param title		标题
	 * @param description		标题描述
	 * @param imgUrl		图片地址
	 * @param userMobile		用户手机号,多个用|分割
	 * @param deptIds		部门号,多个用|分割
	 * @param jumpUrl			点击跳转地址
	 * @return
	 */
	public static String[] pushNews(String accessToken, String agentId,String title, String description, String imgUrl, String userMobiles, String deptIds, String jumpUrl){
		StringBuilder sb = new StringBuilder();
		sb.append("{");		
		if(!JStrKit.isBlank(userMobiles)){
			sb.append("\"touser\":\"");
			sb.append(userMobiles);
			sb.append("\",");
		}
		
		if(!JStrKit.isBlank(deptIds)){
			sb.append("\"toparty\":\"");
			sb.append(deptIds);
			sb.append("\",");
		}		
		sb.append("\"msgtype\":\"news\",\"agentid\":");
		sb.append(agentId);
		sb.append(",\"news\":{\"articles\":[{");
		if(!JStrKit.isBlank(title)){
			sb.append("\"title\":\"");
			sb.append(title);
			sb.append("\",");
		}
		if(!JStrKit.isBlank(description)){
			sb.append("\"description\":\"");
			sb.append(description);
			sb.append("\",");
		}
		if(!JStrKit.isBlank(imgUrl)){
			sb.append("\"picurl\":\"");
			sb.append(imgUrl);
			sb.append("\",");
		}
		sb.append("\"url\":\"");
		sb.append(jumpUrl);
		sb.append("\"}]}}");
		
		String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+accessToken;
		
		String[] strArray = new String[3];
		strArray[0] = url;
		strArray[1] = sb.toString();
		strArray[2] = HttpKit.post(url, sb.toString());
		
		return strArray;
	}	
	
	public static Attach downloadPic(String serverId){
			String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token="
					+ WeixinApi.getEnableToken() + "&media_id=" + serverId;
			System.out.println(urlStr);
			String filePath = PathKit.getWebRootPath() + "/upload/" +serverId + ".jpg";
			try {
				
				
				URL url = new URL(urlStr);
				File newFile = new File(filePath);
				FileUtil.copyURLToFile(url, newFile);
				serverId = serverId + ".jpg";

				Attach attach = new Attach();
				attach.setCreater(1);
				attach.setCreatetime(new Date());

				attach.setName(serverId);
				attach.setStatus(1);
				attach.setUrl("/upload/" +serverId);
				
				int id = Blade.create(Attach.class).saveRtId(attach);
				if(id > 0){
					attach.setId(id);
					return attach;
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
	}
	
	public static Attach downloadVideo(String serverId){
		String urlStr = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token="
				+ WeixinApi.getEnableToken() + "&media_id=" + serverId;
		System.out.println(urlStr);
		String filePath = PathKit.getWebRootPath() + "/upload/" +serverId + ".mp4";
		try {
			
			
			URL url = new URL(urlStr);
			File newFile = new File(filePath);
			FileUtil.copyURLToFile(url, newFile);
			serverId = serverId + ".mp4";

			Attach attach = new Attach();
			attach.setCreater(1);
			attach.setCreatetime(new Date());

			attach.setName(serverId);
			attach.setStatus(1);
			attach.setUrl("/upload/" +serverId);
			
			int id = Blade.create(Attach.class).saveRtId(attach);
			if(id > 0){
				attach.setId(id);
				return attach;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
}
	
}
